package extend;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

/**
 * 扩展单例
 * @author gaopeng
 * @date 2019/12/7 3:46 下午
 */
public class Emperor {

    private static final int MAX_SIZE = 2;

    private String name;
    private static List<Emperor> list = new ArrayList<>(MAX_SIZE);

    static {
        for (int i = 0; i < MAX_SIZE; i++) {
            list.add(new Emperor("皇帝"+i));
        }
    }

    private Emperor(String name){
        this.name=name;
    }

    public static Emperor getInstance(){
        Random random = new Random();
        int i = random.nextInt(MAX_SIZE);
        return list.get(i);
    }

    public void say(String thread){
        System.out.println("我是"+this.name+thread);
    }
}
